<%@page import="domain.BlogFile"%>
<%@page pageEncoding="UTF-8" %>
<%@page import="utils.DateUtils"%>
<%@page import="getools4me.BlogManager"%>
<%@page import="domain.DownloadableFile"%>
<%@page import="getools4me.UserManager"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%
	UserService us = UserServiceFactory.getUserService();
	User user = us.getCurrentUser();
	String thisUrl = request.getRequestURI();
	String loginURL = us.createLoginURL(thisUrl);
	String logoutURL = us.createLogoutURL(thisUrl);
	String target_type = request.getParameter("target_type");
	String op = request.getParameter("op");
	String id = request.getParameter("id");
	String postURL = request.getParameter("postURL");
	
	boolean idIsOk = (id!=null) && id.matches("\\d+");
	String msg_error = null;
%>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/blog/css/common.css" />
<link rel="stylesheet" type="text/css" href="/blog/css/menu.css" />
<script type="text/javascript" src="/blog/js/common.js"></script>

<title>Admin tools</title>
</head>
<body>

<div class="menu">
	<ul>
		<li><a href="/blog/page/home.jsp">Home</a></li>
		<li><a href="/blog/page/all.jsp">All</a></li>
		<li><%if(UserManager.isLogin()){ %><a href="<%=UserManager.getLogoutURL(request)%>">Logout</a><%}else{ %><a href="<%=UserManager.getLoginURL(request)%>">Login</a><%} %></li>
		<li><%if(UserManager.isAdmin()){ %>
		<a href="/blog/page/new.jsp">New</a><%} %>
		</li>
	</ul>
</div>

<div class="center h1">

<%if(user!=null){ %>
Welcome, Mr. <%=user.getNickname() %> !
<br>
<%} %>


<%if(user==null){ %> 
		<a href="<%=loginURL%>">Please Login ...</a>
<%}else if(!user.getEmail().equals("jpgtama@gmail.com")) { %>
		<i>Sorry, you are not admin.</i>
<%}else if("delete".equals(op)) {
	
	if(idIsOk){
		msg_error = id + "<---  File with this id not found!";
		if("blog".equals(target_type)){
			if(BlogManager.getBlogFileById(Long.valueOf(id))!=null){
				BlogManager.deleteBlog(Long.valueOf(id));
				msg_error = null;
				response.sendRedirect(postURL);
			}
		}else if("comment".equals(target_type)){
			if(BlogManager.getBlogCommentFileById(Long.valueOf(id))!=null){
				BlogManager.deleteBlogComment(Long.valueOf(id));
				msg_error = null;
				response.sendRedirect(postURL);				
			}
		}
	}else{
	 	msg_error = id+ "  <--- Id not valid!";
	}
} %>

<%=msg_error %>
<br>

<%if(user!=null){ %>(<a href="<%=logoutURL%>">Logout</a>) <%} %>


</div>

</body>
</html>